package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.util.POIUtils;
import com.atguigu.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/OrderSetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile) {
        try {
            List<String[]> arrayList = POIUtils.readExcel(excelFile);
            //数据类型转换，将集合String[]类型换成OrderSetting对象类型

            List<OrderSetting> dataList = new ArrayList<OrderSetting>();
            for (String[] arrs : arrayList) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(new Date(arrs[0]));
                orderSetting.setNumber(Integer.parseInt(arrs[1]));
                orderSetting.setReservations(0);
                dataList.add(orderSetting);
            }
            orderSettingService.addBatch(dataList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);

        }
    }


}
